package com.cheyun.netsalev3.viewmodel.orderlist

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.order.ClockData
import com.cheyun.netsalev3.bean.order.OrderDetail2
import com.cheyun.netsalev3.bean.order.nodelist.OrderList
import com.cheyun.netsalev3.repository.OrderDetailsRepository
import com.cheyun.netsalev3.view.DriveBlockActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyContentDialogLayoutOrder

/**
 * @author Michael
 * @date  2018/12/19 - 9:41.
 */
class OrderDetailsActivityViewModel(var orderDetailsRepository: OrderDetailsRepository) : BaseViewModel() {

    var clueDetails = ObservableField<OrderDetail2>()
    var pic = MutableLiveData<String>()
//    var orderList = MutableLiveData<List<OrderList>>()
    var carList = MutableLiveData<List<OrderList>>()

    var showType = MutableLiveData<Int>(1)

    var showMap = MutableLiveData<Int>()

    var agreement = MutableLiveData<Boolean>()
    var agreementFinished = MutableLiveData<Int>()
    var mortgage = MutableLiveData<Boolean>()
    var mortgageFinished = MutableLiveData<Int>()
    var handover = MutableLiveData<Boolean>()
    var handoverFinished = MutableLiveData<Int>()
    var license = MutableLiveData<Boolean>()
    var licenseFinished = MutableLiveData<Int>()
    var agreementText = MutableLiveData<String>()
    var mortgageText = MutableLiveData<String>()
    var handoverText = MutableLiveData<String>()
    var licenseText = MutableLiveData<String>()

    var showClock = MutableLiveData<Boolean>()

    var clockList = arrayListOf<ClockData>()

    init {
        initData()
        showType.postValue(1)
        showClock.postValue(false)
    }

    /**
     * 初始化数据
     */
    fun initData() {
        orderDetailsRepository.getData(this::setData)

        orderDetailsRepository.getClockList(this::setClockList)
    }

    private fun setData(item: OrderDetail2) {
        clueDetails.set(item)
        pic.postValue(item.detail.seriespic)

//        var olist = ArrayList<OrderList>()
//        var orderlistShowSize = item.nodelist.orderlist.filter { it.isshow == 1 }.size
        var n = 0
//        for (index in 0 until item.nodelist.orderlist.size){
//            var d = item.nodelist.orderlist.get(index)
//            if(d.isshow == 1){
//                d.isLast = if(n == orderlistShowSize-1) 1 else 0
//                d.isLlicense = if(d.tkey.equals("license")) 1 else 0
//                olist.add(d)
//                n ++
//            }
//        }
//        orderList.postValue(olist)

        var clist = ArrayList<OrderList>()
        var carlistShowSize = item.nodelist.carlist.filter { it.isshow == 1 }.size
        n = 0
        for (index in 0 until item.nodelist.carlist.size){
            var d = item.nodelist.carlist.get(index)
            if(d.isshow == 1){
                d.isLast = if(n == carlistShowSize -1) 1 else 0
                d.isLlicense = if(d.tkey.equals("carfinished")) 1 else 0
                clist.add(d)
                n ++
            }
        }
        carList.postValue(clist)
    }

    fun onChooseType(view: View, type:Int){
        showType.postValue(type)
        if(type == 0){
            showMap.postValue(0)
        }else{
            showMap.postValue(clueDetails.get()?.detail?.mapswitch)
        }
    }

    fun onGoMap(view: View){
        var intent = Intent(view.context, DriveBlockActivity::class.java)
        intent.putExtra("type","orderMap")
        intent.putExtra("tid",clueDetails.get()?.detail?.tid.toString())
        intent.putExtra("channel",clueDetails.get()?.detail?.channel.toString())
        intent.putExtra("id",clueDetails.get()?.detail?.id.toString())
        view.context.startActivity(intent)
    }

    fun setClockList(list: List<ClockData>){
        clockList.clear()
        agreement.postValue(false)
        agreementFinished.postValue(0)

        mortgage.postValue(false)
        mortgageFinished.postValue(0)

        handover.postValue(false)
        handoverFinished.postValue(0)

        license.postValue(false)
        licenseFinished.postValue(0)

        list.forEach {
            if(it.state.equals("agreement")){
                agreement.postValue(true)
                agreementFinished.postValue(it.finished)
                agreementText.postValue(it.title)

                showClock.postValue(true)
            }
            if(it.state.equals("mortgage")){
                mortgage.postValue(true)
                mortgageFinished.postValue(it.finished)
                mortgageText.postValue(it.title)
                showClock.postValue(true)
            }
            if(it.state.equals("handover")){
                handover.postValue(true)
                handoverFinished.postValue(it.finished)
                handoverText.postValue(it.title)
                showClock.postValue(true)
            }
            if(it.state.equals("license")){
                license.postValue(true)
                licenseFinished.postValue(it.finished)
                licenseText.postValue(it.title)
                showClock.postValue(true)
            }
            clockList.add(it)
        }
    }

    var currentClockData:ClockData? = null
    fun showClock(view: View,key:String){
        if(clueDetails?.get()?.detail?.ischange == 5){
            MyToast("已提交至DMS换车中，请先至DMS完成交期答复并在同步DMS订单")
            return
        }

        var context = view.context
        if(clueDetails.get() == null || clockList== null || clockList.size==0){
            return
        }
        if(key.equals("agreement")){
            if(clueDetails?.get()?.detail?.is_servicing == 0){
                //判断完成交车是否已结束 ， 已结束（只能修改上牌服务）
                var hasHandover = false
                var handoverClock = clockList.filter { it.state.equals("handover") }
                if(handoverClock!=null && handoverClock.size>0){
                    hasHandover = handoverClock.get(0).finished == 1
                }
                var clockData = clockList.get(0)
                var dialog = MyContentDialogLayoutOrder.newInstance("完成签署", R.layout.order_clock_layout,true,clockData,hasHandover,clueDetails?.get()?.detail!!)
                dialog.onClickOk = this@OrderDetailsActivityViewModel::confirmClock
                dialog.onClickCancel = this@OrderDetailsActivityViewModel::cancelClock
                dialog.show((context as AppCompatActivity).supportFragmentManager, "")
            }else{
                MyToast("订单已整备完成，无法操作")
            }

        }else{
            var ll = clockList.filter { it.state.equals(key) }
            if(ll== null || ll.size == 0)return
            var clockData = ll.get(0)
            if(key.equals("handover")){
                if(clockData.finished == 1){
                    //完成交付， 已结束 不能再操作
                    return
                }
            }else{
                var handoverClock = clockList.find { it.state.equals("handover") }
                if(handoverClock!=null && handoverClock.finished == 1){
                    return
                }
            }

            currentClockData = clockData
            var dialog = MyContentDialog.newInstance("提示",if(clockData.finished == 1) "是否取消打卡" else "是否完成打卡")
            dialog.onClickOk = this@OrderDetailsActivityViewModel::confirmClock2

            dialog.show((context as AppCompatActivity).supportFragmentManager, "")
        }
    }

    fun cancelClock(){

    }

    fun confirmClock(clockData: ClockData){
        var moth: () -> Unit = {
            orderDetailsRepository.getClockList(this::setClockList)
        }
        var fail: () -> Unit = {

        }
        var payment = ""
        var license = ""
        var operate = if(clockData.finished == 1) "del" else "edit"
        if(clockData.ikey.equals("agreement")){
            payment = clockData.ext.payment
            license = clockData.ext.license
            operate = if(clockData.finished == 1) "edit" else "del"
        }
        orderDetailsRepository.addClock(clockData.id,payment,license,operate,moth,fail)
    }
    fun confirmClock2(){
        confirmClock(currentClockData!!)
    }
}